package rp.lee.callsdecline.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rp.lee.callsdecline.R;
import rp.lee.callsdecline.activity.LogExpandableListActivity;
import rp.lee.callsdecline.core.db.model.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LogExpandableListAdapter extends BaseExpandableListAdapter {

	List< Log > group;
	List< List<Log> > child;
	LogExpandableListActivity mLogExpandableListAct;
	
	LayoutInflater mInflater;
	
	//KEY String Ϊ "groupPosition-childPosition"
	Map<String, View> childCache = new HashMap<String, View>();
	Map<Integer, View> groupCache = new HashMap<Integer, View>();
	
	
	@Override
	public void notifyDataSetChanged() {
		childCache.clear();
		groupCache.clear();
		super.notifyDataSetChanged();
	}

	@Override
	public void notifyDataSetInvalidated() {
		childCache.clear();
		groupCache.clear();
		super.notifyDataSetInvalidated();
	}
	
	public LogExpandableListAdapter(List<Log> group, List<List<Log>> child,
			LogExpandableListActivity mLogExpandableListAct) {
		this.group = group;
		this.child = child;
		this.mLogExpandableListAct = mLogExpandableListAct;
		mInflater = LayoutInflater.from(mLogExpandableListAct);
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return this.child.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		String key = groupPosition + "_" + childPosition;
		if(childCache.containsKey(key)) {
			return childCache.get(key);
		}
		
		final Log childLog = child.get(groupPosition).get(childPosition);
		
		View childView = mInflater.inflate(R.layout.logexpandablelist_item, null);
		ImageView log_iv_delete = (ImageView) childView.findViewById(R.id.log_iv_delete);
		TextView log_tv_description = (TextView) childView.findViewById(R.id.log_tv_description);
		
		log_tv_description.setText(childLog.getDescription() + " [" + childLog.getLogDate() + "]");
		log_iv_delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mLogExpandableListAct.deleteBy_id(groupPosition, childPosition, childLog.get_id());
			}
		});
		
		childCache.put(key, childView);
		return childView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return this.child.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return group.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return group.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		if(groupCache.containsKey(groupPosition)) {
			return groupCache.get(groupPosition);
		}
		
		View groupView = mInflater.inflate(R.layout.logexpandablelist_group, null);
		ImageView log_iv_delete = (ImageView) groupView.findViewById(R.id.log_iv_delete);
		TextView log_tv_name = (TextView) groupView.findViewById(R.id.log_tv_name);
		final Log groupOne = group.get(groupPosition);
		
		String title = groupOne.getName() + "-" + groupOne.getPhoneNumber();
		
		log_iv_delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mLogExpandableListAct.deleteGroupByDeclineListId(groupPosition, groupOne.getDeclinelistId());
			}
		});
		log_tv_name.setText(title);
		
//		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
//		TextView text = new TextView(mLogExpandableListAct);
//		text.setLayoutParams(lp);
//		text.setTextSize(20);
//		text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
//		text.setPadding(36, 0, 0, 0);
		
		groupCache.put(groupPosition, groupView);
		return groupView;
	}

	@Override
	public boolean hasStableIds() {
		System.out.println("hasStableIds>>>>>>>>>");
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		System.out.println("isChildSelectable>>>>>>>>>" + groupPosition + ":" + childPosition);
		return true;
	}

}